<template>
  <div class="home">
    
    <h3 class="page_title">{{homeYear}}校区经营一览</h3>
    
    <div class="content">
      <el-form  label-position="left" class="form">
        <div style="overflow:hidden">
          <el-form-item size="small" prop="schoolId" class="fl" label="校区选择 : " style="width: 50%;">
            <el-select @change="changeSchool" v-model="schoolId" placeholder="校区" style="width: 230px;">
              <el-option v-for="(item,key) in schoolList" :key="key" :label="item.schoolName" :value="item.schoolId">
              </el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>

      <div class="info_box">
        <div class="info_item">
          <div>
            <i class="icon fl"></i>
            <span class="item_title fl">
              <span>本学年目标业绩(元)</span>
            </span>
            <div class="clear"></div>
          </div>
          <div class="num">{{homeData.targetPerformanceForYear}}</div>
        </div>
        <div class="info_item">
          <div>
            <i class="icon fl"></i>
            <span class="item_title fl">
              <span>本学年累计销售额(元)</span>
            </span>
            <div class="clear"></div>
          </div>
          <div class="num">{{homeData.cumulativeSalesForYear}}</div>
        </div>
        <div class="info_item">
          <div>
            <i class="icon fl"></i>
            <span class="item_title fl">
              <span>年度业绩完成率(%)</span>
            </span>
            <div class="clear"></div>
          </div>
          <div class="num">{{homeData.completionRateForYear}}</div>
        </div>
        <div class="info_item">
          <div>
            <i class="icon fl"></i>
            <span class="item_title fl">
              <span>当月销售额(元)</span>
            </span>
            <div class="clear"></div>
          </div>
          <div class="num">{{homeData.monthCumulativeSalesForYear}}</div>
        </div>
      </div>

      <div class="bottom_area">
        <div class="left ">
          <h4 class="title">学员情况</h4>
          <div>
            <i class="icon fl"></i>
            <div class="fl num_box">
              <span class="num">{{homeData.studentNumber}}人</span>
              <span class="txt">已缴费学员数</span>
            </div>
            <div class="clear"></div>
          </div>
          <div>
            <i class="icon fl"></i>
            <div class="fl num_box">
              <span class="num">{{homeData.employeesNumber}}人</span>
              <span class="txt">当前员工数目</span>
            </div>
            <div class="clear"></div>
          </div>
        </div>
        <div class="right ">
          <h4 class="title">{{schoolName}}校区 {{this.year}}学年销售情况按月分布</h4>
          <div class="echarts"  style="width:100%;height:340px;" id="homeEcharts"></div>
        </div>
        <!-- <div class="clear"></div> -->
      </div>



    </div>

  </div>
</template>


<style scoped>
.home{
  /* background: #fff; */
}

.info_box{
  display: flex;
  /* padding: 0 20px; */
  box-sizing: border-box;
  justify-content: space-between;
}
.info_box .info_item{
  width: 220px;
  height: 105px;
  border-radius:16px;
  color: #fff;
  padding: 15px;
  box-sizing: border-box;
  /* box-shadow:0px 0px 4px 0px rgba(211,205,205,0.5); */
}

.info_box .info_item .item_title{
  font-size: 14px;
  padding-left: 15px;
}

.info_box .num {
  text-align: center;
  margin-top: 15px;
  font-size: 18px;
}


.info_box > div:nth-child(1) {
  line-height: 27px;
  background:linear-gradient(293deg,rgba(116,215,254,1) 0%,rgba(148,157,253,1) 100%);
}
.info_box > div:nth-child(2) {
  line-height: 27px;
  background:linear-gradient(293deg,rgba(252,132,169,1) 0%,rgba(251,183,138,1) 100%);
}
.info_box > div:nth-child(3) {
  line-height: 27px;
  background:linear-gradient(293deg,rgba(90,232,172,1) 0%,rgba(113,217,225,1) 100%);
}
.info_box > div:nth-child(4) {
  line-height: 27px;
  background:linear-gradient(117deg,rgba(214,144,230,1) 0%,rgba(254,120,136,1) 100%);
}

.info_box .info_item .icon {
  display: inline-block;
}
.info_box > div:nth-child(1) .icon {
  background: url('../../../assets/img/home_icon3.png') 0 0 no-repeat;
  background-size: 100%;
  width: 25px;
  height: 27px;
}
.info_box > div:nth-child(2) .icon {
  background: url('../../../assets/img/icon2.png') 0 0 no-repeat;
  background-size: 100%;
  width: 25px;
  height: 27px;
}
.info_box > div:nth-child(3) .icon {
  background: url('../../../assets/img/home_icon4.png') 0 0 no-repeat;
  background-size: 100%;
  width: 25px;
  height: 27px;
}
.info_box > div:nth-child(4) .icon {
  background: url('../../../assets/img/home_icon5.png') 0 0 no-repeat;
  background-size: 100%;
  width: 25px;
  height: 27px;
}
.page_title {
  color: #FC8434;
  text-align: center;
  font-size:22px;
  font-weight: 600;
}

.bottom_area {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}
.bottom_area .left {
  width: 30%;
  margin-right: 20px;;
}
.bottom_area .right {
  width: 70%;
}
.bottom_area .title {
  color: #FC8434;
  font-size: 14px;
  font-weight: 550;
  font-family: 'Microsoft Yahei';
  margin-bottom: 15px;
}
.echarts{
  background: #fff;
}
.bottom_area .left > div {
  width: 100%;
  height:160px;
  /* box-shadow:0px 0px 3px 3px rgba(234,232,232,0.5); */
  border-radius:16px;
  padding: 45px 30px;
  box-sizing: border-box;
  background: #fff;
}
 .bottom_area .right > div {
  width: 100%;
  height:340px;
  /* box-shadow:0px 0px 3px 3px rgba(234,232,232,0.5); */
  border-radius:16px;
 }

.bottom_area .left > div:nth-child(2) i{
  display: inline-block;
  background: url('../../../assets/img/home_icon7.png') 0 0 no-repeat;
  background-size: 100%;
  width: 70px;
  height: 70px;
}
.bottom_area .left > div:last-child i{
  display: inline-block;
  background: url('../../../assets/img/home_icon6.png') 0 0 no-repeat;
  background-size: 100%;
  width: 70px;
  height: 70px;
}

.bottom_area .num_box {
  margin-left: 30px;
}
.bottom_area .left > div:nth-child(2) .num {
  color: #8B9DFB;
}
.bottom_area .left > div:last-child .num {
  color: #FE8697;
}
.bottom_area .left > div span {
  display: block;
}
.bottom_area .num {
  font-size: 20px;
  margin-top: 5px;
}
.bottom_area .txt {
  font-size: 14px;
  color: #6D7278;
  margin-top: 15px;
  font-weight: 500;
}


.bottom_area .left > div:last-child {
  margin-top: 20px;
}

.bottom_area .left .title {
  line-height: 28px;
  margin-left: 10px;
}
.bottom_area .left .title:before {
  content: '';
  display: inline-block;
  background: url('../../../assets/img/home_icon1.png') 0 0 no-repeat;
  background-size: 100%;
  width: 23px;
  height: 28px;
  vertical-align: middle;
  margin-right: 10px;
}

.bottom_area .right .title {
  line-height: 25px;
  margin-left: 10px;
}
.bottom_area .right .title:before {
  content: '';
  display: inline-block;
  background: url('../../../assets/img/home_icon2.png') 0 0 no-repeat;
  background-size: 100%;
  width: 28px;
  height: 28px;
  vertical-align: middle;
  margin-right: 10px;
}


</style>

<script>
import {
  createNamespacedHelpers
} from "vuex";
let {
  mapState,
  mapGetters,
  mapMutations,
  mapActions
} = createNamespacedHelpers("home");


export default {
  name: 'home',
  data() {
    return {
      jwtAuth: {},
      year: '',
      schoolId: '',
      schoolName: '',
      schoolList: [],
      homeData: {
        
      },
    }
  },
  created() {
    
    
    let date = new Date();
    this.year = date.getFullYear();
    let month = date.getMonth() + 1;
    if(month < 9) {
      this.year -= 1
    }
    // console.log(month);
  },
  mounted() {
    console.log(this.schoolId);
    this.jwtAuth = JSON.parse(sessionStorage.getItem('jwtAuth'))
    this.queryAllSchool();
    
  },
  
  methods: {
    ...mapMutations([
      "updateInfo",
    ]),
    ...mapActions([
      "formGet",
      "post"
    ]),

    queryAllSchool() {
      let accountId
      this.jwtAuth.schoolId == '' ? accountId = '' : accountId = this.jwtAuth.accountId
      this.post({
        API: 'queryAllSchool',
        data: {
          accountId: accountId,
          menuId: '',
          // menuId: '1110',
          schoolIds: this.jwtAuth.schoolId.split(','),
        }
      }).then(res => {
        this.schoolList = res
        
        this.schoolId = this.schoolList[0].schoolId;
        this.getSchoolInfo(this.schoolId);
        this.initData();
      }).catch(err => {
        this.$message.error(err)
      })
    },

    changeSchool(e) {
      this.getSchoolInfo(e);
      
      if(e == '') {
        this.schoolId = ''
      }
      this.initData()
      
    },

    getSchoolInfo(id) {
      this.schoolList.forEach(item => {
        if(item.schoolId == id) {
          this.schoolName = item.schoolName
        }

        if(item.schoolName == '总服务部') {
          item.schoolId = '';
        }
      })
    },


    initData() {
      this.formGet({
        API: 'homeScreenDisplay',
        data: {
          schoolId: this.schoolId
        }
      }).then(res => {
        this.homeData = res;
        this.initEcharts(res.bmTargetYearChildList);
      }).catch(err => {
        this.$message.error(err);
      })
    },
    

    initEcharts(data) {
      let myChart = this.$echarts.init(document.getElementById("homeEcharts"));
      let targetData = [];
      let completeData = [];
      let monthCumulativeCompleteData = [];
      let monthData = ['9月', '10月', '11月', '12月', '1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月']
      
      let date = new Date();
      let currentMonth = (Number(date.getMonth()) + 1);
      let currentYear = date.getFullYear();
      console.log(currentMonth)
      if(currentMonth < 9) {
        currentMonth += 12;
        monthData[0] = `${currentYear-1}年9月`
        monthData[4] = `${currentYear}年1月`
      }
      let currentMonthIndex = currentMonth - 9

      data.forEach((item, index) => {
        if(index <= currentMonthIndex) {
          completeData.push(item.monthComplete)
        }
        monthCumulativeCompleteData.push(item.monthCumulativeComplete)
        targetData.push(item.monthTargetPrice)

      })

      let option = {
          color: ['#FBA56C','#D690E6', '#8B9DFB'],
          grid: {
            left: '3%',
            right: '3%',
            bottom: '5%',
            containLabel: true
          },
          legend: {       // 图例
            data: ['年度规划分月目标(元)','月度拆解目标(元)', '本月实际完成业绩(元)'],
            icon: 'circle',
            top: '5%',
            right: '5%',
            textStyle: {
              color: "#485465",
              fontSize: 12,
              fontWeight: 400,
            },
            itemWidth: 14,
            itemHeight: 14,
            itemGap: 35,
          },
          tooltip : {
            trigger: 'axis',
            axisPointer : {  
              type : 'shadow',
              shadowStyle: {
                color: 'rgba(150,150,150,0.15)',
              } 
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisLabel: {        // x轴 标签
              show: true,
              textStyle: {
                color: "#485465",
                fontSize: 12,
                fontWeight: 400
              }
            },
            data: monthData
          },
          yAxis: {
            type: 'value',
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisLabel: {        // x轴 标签
              show: true,
              textStyle: {
                color: "#485465",
                fontSize: 12,
                fontWeight: 400
              }
            }
          },
          series: [
            {
              name: '年度规划分月目标(元)',
              data: targetData,
              type: 'line',
              smooth: 0.4,
              symbol: 'none',
              lineStyle: {
                width: 1.5,
              },
              areaStyle: {
                  normal: {
                      color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                          offset: 0,
                          color: '#FCDBC4'
                      }, {
                          offset: 1,
                          color: '#fff'
                      }])
                  }
              },
            },
            {
              name: '月度拆解目标(元)',
              data: monthCumulativeCompleteData,
              type: 'line',
              smooth: 0.4,
              symbol: 'none',
              lineStyle: {
                width: 1.5,
              },
              areaStyle: {
                  normal: {
                      color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                          offset: 0,
                          color: '#D690E6'
                      }, {
                          offset: 1,
                          color: '#fff'
                      }])
                  }
              },
            },
            {
              name: '本月实际完成业绩(元)',
              data: completeData,
              type: 'line',
              smooth: 0.4,
              symbol: 'none',
              lineStyle: {
                width: 1.5,
              },
              areaStyle: {
                  normal: {
                      color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                          offset: 0,
                          color: '#C1CAFA'
                      }, {
                          offset: 1,
                          color: '#fff'
                      }])
                  }
              },
            },
            
          ]
      };




      myChart.setOption(option, true);
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    }
    
  },
  computed: {
    ...mapState([
    ]),
    homeYear() {
      let year = new Date().getFullYear();
      let month = new Date().getMonth() + 1;

      if(month < 9) {
        return `${year-1}年9月至${year}年8月`
      }else{
        return `${year}年9月至${year+1}年8月`
      }
    }
    // ...mapGetters(["selectedIds"])
  },
  components: {
  },
}

</script>